package com.tencent.wxcloudrun.service.impl;

import com.tencent.wxcloudrun.dto.*;
import com.tencent.wxcloudrun.mapper.GoodsMapper;
import com.tencent.wxcloudrun.mapper.OrderMapper;
import com.tencent.wxcloudrun.mapper.SkulistMapper;
import com.tencent.wxcloudrun.service.OrderService;
import com.tencent.wxcloudrun.service.OrdergoodsService;
import com.tencent.wxcloudrun.service.OrderkdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2023/3/16 15:16
 * @description: TODO
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrdergoodsService ordergoodsService;

    @Autowired
    private OrderkdService orderkdService;

    @Autowired
    private SkulistMapper skulistMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public List<Order> orderList(String openid) {
        return orderMapper.orderList(openid);
    }

    /**
     * 订单，商品，快递地址
     * @param openid
     * @param json
     * @return
     */
    @Override
    public String addOder(String openid, OrderCreateRequest json) {
        String orderid = getOrderIdByTime();
        String shopId = json.getShopIdZt();
        List<OrderCreateGoods> goodsJsonStr = json.getGoodsJsonStr();
        Integer amout = 0;
        Integer numbers = 0;
        for (int i = 0; i < goodsJsonStr.size(); i++) {
            Integer number = goodsJsonStr.get(i).getNumber();
            Integer goodsId = goodsJsonStr.get(i).getGoodsId();
            Goods goods = goodsMapper.getById(String.valueOf(goodsId));
            Integer minprice = goods.getMinprice();
            amout = amout + number * minprice;
            numbers = numbers + number;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        String newDate = sdf.format(new Date());
        //订单主信息
        orderMapper.addOder(orderid, openid, shopId,amout,newDate,numbers);
        //快递地址信息
        orderkdService.addOderkd(orderid,json);
        //订单商品信息
        ordergoodsService.addOrderGoods(goodsJsonStr, orderid);
        ExtJsonStr extJsonStr = json.getExtJsonStr();
        //自取联系信息
        if (extJsonStr != null) {
            String zqmobile = extJsonStr.getZqmobile();
            String zqdiningTime = extJsonStr.getZqdiningTime();
            orderMapper.addExtJsonStr(orderid, zqmobile, zqdiningTime);
        }
        return orderid;
    }

    @Override
    public Map<String,Integer> getOrderGoodsNumber(String orderid) {
        Map<String,Integer> map = new HashMap<>();
        List<Ordergoods> goodsNumber = ordergoodsService.getGoodsNumber(orderid);
        int k = 0;
        int j = 0;
        for (int i = 0; i < goodsNumber.size(); i++) {
            k = k + goodsNumber.get(i).getNumber();
            j = j + goodsNumber.get(i).getAmout();
        }
        map.put("goodsNumber",k);
        map.put("amountTotle",j);
        return map;
    }

    @Override
    public int cyTableAddOrder(String orderid,String openid, Integer goodsId, Integer number, String propertyChildIds, String key) {
        //桌子code
         orderMapper.addOder(orderid,goodsId,number,propertyChildIds,key);
        return 0;
    }

    @Override
    public int cyTableAddOrder(String orderId, String openid, String tableId, String key) {
        int i = orderMapper.cyTableAddOrder(orderId, openid, tableId, key);
        return i;
    }

    @Override
    public int addOrderGoods(String orderId, Integer goodsId, Integer number, String propertyChildIds){
        GoodsPrice price = skulistMapper.getGoodsPrice(goodsId, propertyChildIds);
        Integer amout = price.getPrice();
        Integer score = price.getScore();
        int i = orderMapper.addOrderGoods(orderId, goodsId, number, amout, score);
        return i;
    }

    @Override
    public int addOrderGoods(String orderId, Integer goodsId, Integer number) {
        return 0;
    }

    @Override
    public Order orderDetail(String orderId) {
        return orderMapper.orderDetail(orderId);
    }


    //生成订单号22位
    private String getOrderIdByTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String newDate = sdf.format(new Date());
        String result = "";
        Random random = new Random();
        for (int i = 0; i < 3; i++) {
            result += random.nextInt(10);
        }
        return newDate + result;
    }
}
